package com.webfinal.backsystem.interceptor;

import com.webfinal.backsystem.constant.AuthConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AuthInterceptor implements HandlerInterceptor {
    private static Logger LOG= LoggerFactory.getLogger(AuthInterceptor.class);

//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
//        LOG.info("################权限认证开始##################");
//        LOG.warn("################权限认证开始##################");
//        handleResponse(response);
//        String token = request.getHeader(AuthConstant.ACCOUNT);
//        if (token == null || token.equals("")){
//            throw new Exception("token信息缺失，认证失败");
//        }
//
//        LOG.info("################权限认证成功##################");
//        LOG.warn("################权限认证失败##################");
//        LOG.info("################权限认证结束##################");
//
//        return true;
//    }

    private void handleResponse(HttpServletResponse response) throws Exception{
        if (response.getStatus() == Integer.valueOf("401")){
            throw new Exception("拦截器过滤，认证失败");
        } else if (response.getStatus() == Integer.valueOf("403")) {
            throw new Exception("禁止登录");
        } else if (response.getStatus() == Integer.valueOf("404")) {
            throw new Exception("未知请求");
        } else if (response.getStatus() == Integer.valueOf("500")) {
            throw new Exception("服务出错");
        }
    }

}
